<?php
namespace OrderAdmin\Controller;
use OrderAdmin\Model\OrderModel;
use OrderAdmin\Model\WorkerModel;
use Think\PageAdmin;

/**
 * 师傅管理控制器
 */
class WorkerController extends BaseController
{

    protected $workerModel = null;

    /**
     * 初始化
     *
     * @return [type] [description]
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->workerModel = new WorkerModel();
        $this->orderModel = new OrderModel();
    }

   	/**
   	 * 师傅列表
   	 * @return [type] [description]
   	 */
    public function index(){
        $count = $this->workerModel->getAllCount();      //总数
        $thisMonthCount = $this->workerModel->getCountThisMonth();
        $this->seotitle = '师傅列表';
        $this->assign(compact( 'count', 'thisMonthCount'));
        $this->display();
    }

    public function getData(){
        $param['province'] = I('post.province', '');
        $param['city'] = I('post.city', '');
        $param['key'] = I('post.key', '');
        $param['page'] = I('post.page', 1, 'intval');
        $param['size'] = I('post.size', 10, 'intval');
        $status = I('post.status', '');
        if($status !== ''){
            $param['status'] = explode(',', $status);
        }
        $data = $this->workerModel->getData($param);
        ajax_return($data['status'], '', $data['data']);
    }

//    /**
//     * 师傅详情
//     */
//    public function detail(){
//        $id = I('get.id', '');
//        //$id = 12;
//        $info = M('worker w')
//                ->join("wk_partner p on w.partner_id=p.id")
//                ->field('w.*,p.name partner_name')
//                ->where(['w.id' => $id])->find();
//
//
//        if (!$info) {
//            $this->error('id有误！');
//        }
//
//        $count = M('order')->count();
//        $page  = new PageAdmin($count, C('PAGE_NUM'));
//
//        $pages['show']  = $page->show();  //分页输出
//        $pages['total'] = $count;  //总数
//        $pages['lists'] = M('order a')
//            ->order("a.id desc")
//            ->where("a.worker_id=".$id)
//            ->limit($page->firstRow.','.$page->listRows)
//            ->select();
//
//        $this->pages    = $pages;
//
//        $this->assign('info', $info);
//        $this->assign('order', $order);
//        $this->seotitle = '师傅详情';
//        $this->display();
//    }

    /**
     * 师傅详情
     */
    public function detail(){
        $id = I('get.id', '');
        //$id = 12;
        $info = M('worker w')
            ->join("wk_partner p on w.partner_id=p.id")
            ->field('w.*,p.name partner_name')
            ->where(['w.id' => $id])->find();

        if (!$info) {
            $this->error('id有误！');
        }

        $this->assign(compact('info'));
        $this->seotitle = '师傅详情';
        $this->display();
    }

    /**
     * 审核师傅主页
     */
    public function checkinfo(){
        $this->display();
    }

    public function checkpage(){
        $id = I('get.id', 0, 'intval');
        if($id === 0){
            $this->error('师傅信息不存在');
        }
        $data = $this->workerModel->getInfoById($id);
        if($data){
            $this->assign(compact('data'));
            $this->display();
        }else{
            $this->error('获取信息失败');
        }
    }

    public function check(){
        $id = I('post.id', 0, 'intval');
        $status = I('status');
        $audit_info = I('audit_info');

        if(!$id || !$status || !$audit_info){
            $this->ajaxReturn(['status' => false, 'code' => 1001, 'msg' => '参数异常']);
        }

        $data = [
                    'status'=>$status,
                    'audit_info'=>$audit_info,
                    'audit_time'=>time()
                ];

        $r = M("worker")->where(['id'=>$id])->save($data);

        if($r){
            $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '审核成功']);
        }else{
            $this->ajaxReturn(['status' => false, 'code' => 1000, 'msg' => '系统错误']);
        }
    }

    /**
     * 获取全部可用的师傅
     */
    public function getWorkers(){
        $data = M('worker')->field('id, name')->where(['status'=>'3'])->order('name asc')->select();
        ajax_return(true, '', $data);
    }
    

}